###############################################################################
# Makefile for the project FanControl
###############################################################################

## General Flags
PROJECT = FanControl
MCU = atmega128
TARGET = FanControl.elf
CC = avr-gcc

CPP = avr-g++

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2 -std=gnu99                                   -DF_CPU=16000000UL -O3 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d 

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=FanControl.map


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings


## Include Directories
INCLUDES = -I"C:\Users\staehrm\Documents\MC\PCCtrl\LowLevelDriver\include" -I"C:\Users\staehrm\Documents\MC\PCCtrl\HighLevelDriver\include" -I"C:\Users\staehrm\Documents\MC\PCCtrl\Application\include" 

## Objects that must be built in order to link
OBJECTS = Uart.o Timer0.o Timer1.o Timer2.o Timer3.o TWI.o Main.o LM75.o RTC.o ComProtocol.o FanRpm.o Relay.o FanPwm.o ADC.o WaterTemp.o DIM230.o 

## Objects explicitly added by the user
LINKONLYOBJECTS = 

## Build
all: $(TARGET) FanControl.hex FanControl.eep FanControl.lss size

## Compile
Uart.o: ../LowLevelDriver/src/Uart.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Timer0.o: ../LowLevelDriver/src/Timer0.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Timer1.o: ../LowLevelDriver/src/Timer1.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Timer2.o: ../LowLevelDriver/src/Timer2.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Timer3.o: ../LowLevelDriver/src/Timer3.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

TWI.o: ../LowLevelDriver/src/TWI.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Main.o: ../Application/src/Main.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

LM75.o: ../HighLevelDriver/src/LM75.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

RTC.o: ../HighLevelDriver/src/RTC.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ComProtocol.o: ../HighLevelDriver/src/ComProtocol.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

FanRpm.o: ../HighLevelDriver/src/FanRpm.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Relay.o: ../HighLevelDriver/src/Relay.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

FanPwm.o: ../HighLevelDriver/src/FanPwm.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ADC.o: ../LowLevelDriver/src/ADC.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

WaterTemp.o: ../HighLevelDriver/src/WaterTemp.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

DIM230.o: ../HighLevelDriver/src/DIM230.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
	 $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
	-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
	avr-objdump -h -S $< > $@

size: ${TARGET}
	@echo
	@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
	-rm -rf $(OBJECTS) FanControl.elf dep/* FanControl.hex FanControl.eep FanControl.lss FanControl.map


## Other dependencies
-include $(shell mkdir dep 2>NUL) $(wildcard dep/*)

